package com.hanoi.wiki.controller.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanoi.wiki.controller.UserStatisticController;
import com.hanoi.wiki.exception.FunctionalException;
import com.hanoi.wiki.manager.UserStatisticManager;
import com.hanoi.wiki.model.json.BaseJsonObject;
import com.hanoi.wiki.model.json.ResponseJsonObject;
import com.hanoi.wiki.model.json.UserStatisticJson;

@Controller
@RequestMapping("/uss")
public class UserStatisticControllerImpl extends BaseControllerImpl implements UserStatisticController{

	@Autowired
	private UserStatisticManager statisticManager;
	
	public @ResponseBody ResponseJsonObject saveUserStatistic(@RequestBody UserStatisticJson userStatistic) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		int id = 0;
		try {
			id = statisticManager.save(userStatistic);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		userStatistic.setId(id);
		objects.add(userStatistic);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject removeUserStatistic(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		UserStatisticJson json = null;
		try {
			json = statisticManager.get(id);
			statisticManager.remove(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject updateUserStatistic(@RequestBody UserStatisticJson userStatistic) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			statisticManager.update(userStatistic);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(userStatistic);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getUserStatistic(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		UserStatisticJson json = null;
		try {
			json = statisticManager.get(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getUserStatisticList() {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			objects.addAll(statisticManager.getList());
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

}
